package com.briup.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * DQL的使用
 * ResultSet的使用
 */
public class DQLTest {
    public static final String driver = "com.mysql.cj.jdbc.Driver";
    public static final String url = "jdbc:mysql:///db01";
    public static final String username = "root";
    public static final String password = "rootroot";
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
            // 创建语句对象
            statement = connection.createStatement();
            // 编写sql语句
            String sql = "select * from t_user";
            // 执行查询语句
            ResultSet resultSet = statement.executeQuery(sql);
            // 迭代获取每一行的数据
            List<TUser> list = new ArrayList<>();
            while (resultSet.next()){
                // 获取每一列的数据
                // 1、根据列的序号 2、根据列名
                int id = resultSet.getInt(1);
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                // 输出
//                System.out.println("id = " + id);
//                System.out.println("name = " + name);
//                System.out.println("age = " + age);
//                System.out.println("---------------");
                TUser tUser = new TUser(id, name, age);
                list.add(tUser);
            }
            list.forEach(System.out::println);
            resultSet.close();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if(statement!=null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
class TUser{
    private int id;
    private String name;
    private int age;

    public TUser() {
    }

    public TUser(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "TUser{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
